package com.shermanzhou.springbootflowable.demo.custom.service;

import com.shermanzhou.springbootflowable.demo.custom.cmd.DeleteFlowableProcessInstanceCmd;
import org.flowable.engine.ProcessEngine;

/**
 * @author zhouchaoqun02
 * @date 2020-08-18 20:53
 */
public class ProcessInstanceDeleteUtil {

    public static void deleteProcessInstanceById(ProcessEngine processEngine, String processInstanceId) {
        long count = processEngine.getRuntimeService().createProcessInstanceQuery().processInstanceId(processInstanceId).count();
        if (count > 0) {
            DeleteFlowableProcessInstanceCmd cmd = new DeleteFlowableProcessInstanceCmd(processInstanceId, "删除流程实例", true);
            processEngine.getManagementService().executeCommand(cmd);
        } else {
            processEngine.getHistoryService().deleteHistoricProcessInstance(processInstanceId);
        }
    }
}
